
function muSwiper(option1,option2){
	//上面的列表
function titleList(){
	var fn = {};
	var $engin = null;
	var $wrap = null;
	var $item = null;
	var $num = null;
	var $len = null;
	var $index = 0;
	var $oldIndex = $index;
	var $itemWidth = null;
	fn.createNew = function(eng,wrap,item,num){
		$engin = eng;
		$wrap = $(wrap);
		$item = $wrap.find(item);
		$num = num || 4;
		$len = $item.length;
		
		layOut();
		eventInit(eng,wrap,item);
		
				
	};
	fn.watch = function(Msg){
		if(Msg.dataType == "title"){
			$index = Msg.index;
			change(Msg.index);
		}
	}
	
	function eventInit(eng,wrap,item){
		var $startX = 0;
		var $endX = 0;
		var $miss = 0;
		//点击事件
		$wrap.on("click",item,function(){
			//$(this).addClass("active").siblings().removeClass("active");
			
			$index = $(this).index();
			
			change($index);
			
			$engin.upDate({
				dataType:"slide",
				index:$(this).index()
			});
		});
		
		//滑动事件
		$wrap.on("touchstart",function(e){
			
			$startX = e.originalEvent.targetTouches[0].clientX;
		});
		$wrap.on("touchend",function(e){
			//console.log( e.originalEvent.changedTouches[0]);
			$endX =  e.originalEvent.changedTouches[0].clientX;
			
			$miss = $endX - $startX;
			//console.log($miss)
			if($miss < -50){
				console.log("左滑");
				console.log($index)
				if($index < $len-1){
					change(++$index);
					$engin.upDate({
						dataType:"slide",
						index:$index
					});
				}
				
			}else if($miss > 50){
				console.log("右滑");
				console.log($index)
				if($index > 0){
					change(--$index);
					$engin.upDate({
						dataType:"slide",
						index:$index
					});
				}
			}
		});
		
	}
	
	function change(index){
		
		$item.eq(index).addClass("active").siblings().removeClass("active");
					
		/*if($oldIndex < $index){
			
				$wrap.stop(true,true).animate({
				"left":-$itemWidth*index
				},1000);
			
		}else{
			
			if(index == 0 ){
				$wrap.stop(true,true).animate({
				"left":-$itemWidth*(index)
				},1000);
			}else{
				$wrap.stop(true,true).animate({
				"left":-$itemWidth*(index-1)
				},1000);
			}
				
			
		}*/
		if(index < $num - 1 ){
			$wrap.stop(true,true).animate({
				"left":0
				},1000);
		}else if($len - index  <= $num){
			$wrap.stop(true,true).animate({
				"left":-$itemWidth*($len -$num)
				},1000);
		}else{
			if($oldIndex < $index){
				$wrap.stop(true,true).animate({
				"left":-$itemWidth*index
				},1000);
			}else{
				if(index == 0 ){
					$wrap.stop(true,true).animate({
					"left":-$itemWidth*(index)
					},1000);
				}else{
					$wrap.stop(true,true).animate({
					"left":-$itemWidth*(index-1)
					},1000);
				}
			}
		}
			
			$oldIndex = $index;
		
	}
	
	function layOut(){
		var $winWidth = $(window).width()/$num;
		$item.css("width",$winWidth)
		
		$itemWidth = $winWidth;
	}
	
	return fn;
}

//下面的轮播图

function slideBox(){
	var fn = {};
	var $engin = null;
	var $wrap = null;
	var $item = null;
	var $index = 0;
	var $itemWidth = null;
	var $len = null;
	var $oldIndex = $index;
	fn.createNew = function(eng,wrap,item){
		$engin = eng;
		$wrap = $(wrap);
		$item = $wrap.find(item);
		$len = $item.length;
		layOut(eng,wrap,item);
		eventInit();
	};
	
	fn.watch = function(Msg){
		if(Msg.dataType == "slide"){
			$index = Msg.index;
			change(Msg.index);
		}
	};
	
	function change(index){
			
			$wrap.stop(true,true).animate({"left":-$itemWidth*index},1000);
	}
	
	function layOut(){
		var $winWidth = $(window).width();
		$item.css("width",$winWidth);
		$itemWidth = $winWidth;
	}
	
	function eventInit(eng,wrap,item){
		//滑动事件
		var $startX = 0;
		var $endX = 0;
		var $miss = 0;
		$wrap.on("touchstart",function(e){
			
			$startX = e.originalEvent.targetTouches[0].clientX;
		});
		$wrap.on("touchend",function(e){
			//console.log( e.originalEvent.changedTouches[0]);
			$endX =  e.originalEvent.changedTouches[0].clientX;
			
			$miss = $endX - $startX;
			//console.log($miss)
			if($miss < -50){
//				console.log("左滑");
//				console.log($index);
				if($index < $len-1){
					change(++$index);
					$engin.upDate({
						dataType:"title",
						index:$index
					});		
				}
				
			}else if($miss > 50){
//				console.log("右滑");
//				console.log($index)
				if($index > 0){
					change(--$index);
					$engin.upDate({
						dataType:"title",
						index:$index
					});		
				}
			}
		});
	}
	
	return fn;
}


function engine(){
	var fn = {};
	var coms = [];
	fn.register = function(com){
		coms.push(com);
	};
	
	fn.send = function(Msg){
		for(var i = 0; i < coms.length; i++){
			coms[i].watch(Msg);
			}
		
	};
	
	fn.upDate = function(Msg){
		fn.send(Msg);
	};
	
	
	return fn;
}
		
				
var comsTitle = titleList();
var comsSlideBox = slideBox();
var Engine = engine();

comsTitle.createNew(Engine,option1.wrap,option1.items,option1.num);
comsSlideBox.createNew(Engine,option2.wrap,option2.items);

Engine.register(comsTitle);
Engine.register(comsSlideBox);
		
}
